﻿#include "stdafx.h"
#include "XImageIntParser.h"

#include "XBSTR.h"
#include "XString.h"
#include "StrUtil.h"
#include "Util.h"
#include "IXImage.h"
#include "ParseHelper.h"

CXImageIntParser::CXImageIntParser() : CXIntParserBase(&TIntCreateControl<IXImage>)
{
    ;
}

CXImageIntParser::~CXImageIntParser()
{
    ;
}

// 填充属性
BOOL CXImageIntParser::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl)
{
    BOOL bResult = m_ParentParser.FillAttr(pNode, pControl);
    if(!bResult)
        return bResult;

    XBSTR bstrTemp;
    XString strTemp;
    LONG lTemp;
    BOOL bTemp;
    SIZE sizeTemp;
    COLORREF clrTemp;
    HRESULT hResult;

    IXImage* pImage = Util::QueryIntf<IXImage>(pControl);
    if(pImage == NULL)
        return FALSE;

    // Icon First.
    hResult = pNode->GetValue(_T("icon"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        BOOL bBig = TRUE;
        hResult = pNode->GetValue(_T("big"), &bTemp);
        if(SUCCEEDED(hResult))
            bBig = bTemp;
        pImage->put_Icon(bstrTemp, bBig);
    }

    // Image
    if(FAILED(hResult))
    {
        if(SUCCEEDED(pNode->GetValue(_T("image"), &bstrTemp)))
        {
            pImage->put_Image(bstrTemp);
        }
    }

    // SubCount
    if(SUCCEEDED(pNode->GetValue(_T("count"), &sizeTemp)))
    {
        pImage->put_ImageSubCount(sizeTemp.cx, sizeTemp.cy);
    }

    // ImageIndex
    if(SUCCEEDED(pNode->GetValue(_T("index"), &sizeTemp)))
    {
        pImage->put_ImageIndex(sizeTemp.cx, sizeTemp.cy);
    }

    // AlphaMode
    if(SUCCEEDED(pNode->GetValue(_T("mode"), &bstrTemp)))
    {
        struct stModeInfo
        {
            LPCTSTR          szModeName;
            eImageAlphaMode  mode;
        } data[] =
        {
            {_T("bitblt"),      Alpha_BitBlt},
            {_T("defAlpha"),    Alpha_DefinedAlpha},
            {_T("AlphaVal"),    Alpha_AlphaValue},
            {_T("TransColor"),  Alpha_TransColor},
            {_T("TransAlpha"),  Alpha_TransAlpha},
        };
        for(int i=0; i<_countof(data); ++ i)
        {
            if(_tcsicmp(data[i].szModeName, (LPCTSTR)(BSTR)bstrTemp) == 0)
            {
                pImage->put_ImageAlphaMode(data[i].mode);
                break;
            }
        }
    }

    // AlphaValue
    if(SUCCEEDED(pNode->GetValue(_T("alphaValue"), &lTemp)))
    {
        pImage->put_ImageAlphaValue(lTemp & 0xFF);
    }

    // TransparentColor
    if(SUCCEEDED(pNode->GetValue(_T("clrTrans"), &clrTemp)))
    {
        pImage->put_ImageTransparentColor(clrTemp);
    }

    // DrawType
    if(SUCCEEDED(pNode->GetValue(_T("drawType"), &bstrTemp)))
    {
        struct stLayoutInfo
        {
            LPCTSTR       szDrawTypeName;
            ImageDrawType type;
        } data[] =
        {
            {_T("plain"),       IDT_Plain},
            {_T("9grids"),      IDT_JiuGong},
            {_T("centerFixed"), IDT_CenterFixed},
            {_T("horzTile"),    IDT_HorzTile},
            {_T("vertTile"),    IDT_VertTile},
        };
        XString strToken;
        XString strText(bstrTemp);
        size_t handle = 0;
        DWORD dwType = 0;
        while(StrUtil::NextToken(strText, handle, _T(";"), strToken, FALSE))
        {
            for(int i=0; i<_countof(data); ++ i)
            {
                if(_tcsicmp(data[i].szDrawTypeName, strToken.GetData()) == 0)
                {
                    dwType |= data[i].type;
                }
            }
        }
        if(dwType != 0)
            pImage->put_DrawType((ImageDrawType)dwType);
    }

    // ImageDrawParams
    if(SUCCEEDED(pNode->GetValue(_T("params"), &bstrTemp)))
    {
        ImageDrawParams params = {0};
        if(ParseHelper::ParseImageDrawParams(bstrTemp, params))
            pImage->put_DrawParams(&params);
    }

    return bResult;
}
